package com.sim.simserial.ui

import android.widget.TextView
import androidx.core.os.bundleOf
import androidx.fragment.app.setFragmentResult
import com.drake.tooltip.toast
import com.infinit.selfhelp.common.base.ViewBindingFragment
import com.sim.simserial.R
import com.sim.simserial.databinding.FragmentCoffeeDetailBinding
import com.sim.simserial.navigation.MainNavigator
import com.sim.simserial.view.SizeVIew
import com.sim.storage.GoodsBean
import com.sim.storage.enums.EXTRAEnum
import com.sim.storage.enums.MILKTYPEEnum
import com.sim.storage.enums.SizeEnum
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class CoffeeDetailFragment : ViewBindingFragment<FragmentCoffeeDetailBinding>(R.layout.fragment_coffee_detail) {

    @Inject
    lateinit var homeNavigator: MainNavigator
    val listViews = mutableListOf<SizeVIew>()

    val listExtra = mutableListOf<TextView>()
    val listExtraType = mutableListOf<TextView>()

    var goodsBean: GoodsBean? = null

    override fun initData() {
        super.initData()
        arguments?.getParcelable<GoodsBean>("goods")?.let {
            goodsBean = it
        }
    }

    override fun initView() {
        super.initView()
        mBinding.sizeShort.mBinding.tvSizeName.text = "Short"

        mBinding.sizeTall.mBinding.tvSizeName.text = "Tall"
        val layoutParams = mBinding.sizeTall.mBinding.imgSize.layoutParams
        layoutParams.width = (layoutParams.width * 1.2).toInt()
        mBinding.sizeTall.mBinding.imgSize.layoutParams = layoutParams

        mBinding.sizeGrande.mBinding.tvSizeName.text = "Grande"
        val layoutParams2 = mBinding.sizeGrande.mBinding.imgSize.layoutParams
        layoutParams2.width = (layoutParams2.width * 1.4).toInt()
        mBinding.sizeGrande.mBinding.imgSize.layoutParams = layoutParams2

        mBinding.sizeVenti.mBinding.tvSizeName.text = "Venti"
        val layoutParams3 = mBinding.sizeVenti.mBinding.imgSize.layoutParams
        layoutParams3.width = (layoutParams3.width * 1.6).toInt()
        mBinding.sizeVenti.mBinding.imgSize.layoutParams = layoutParams3

        listViews.apply {
            add(mBinding.sizeShort)
            add(mBinding.sizeTall)
            add(mBinding.sizeGrande)
            add(mBinding.sizeVenti)
        }
        listExtra.apply {
            add(mBinding.extraSugar)
            add(mBinding.extraMilk)
        }
        listExtraType.apply {
            add(mBinding.extraTypeQAT)
            add(mBinding.extraTypeSOY)
            add(mBinding.extraTypeALMOND)
        }

        goodsBean?.let {
            when (it.sizeEnum) {
                SizeEnum.SHORT -> {
                    val sizeView = listViews.first()
                    sizeView.mBinding.cl.setBackgroundResource(R.drawable.bg_coffee_check)
                    sizeView.mBinding.imgSize.setImageResource(R.mipmap.ic_size_select)
                }

                SizeEnum.TALL -> {
                    val sizeView = listViews[1]
                    sizeView.mBinding.cl.setBackgroundResource(R.drawable.bg_coffee_check)
                    sizeView.mBinding.imgSize.setImageResource(R.mipmap.ic_size_select)
                }

                SizeEnum.GRANDE -> {
                    val sizeView = listViews[2]
                    sizeView.mBinding.cl.setBackgroundResource(R.drawable.bg_coffee_check)
                    sizeView.mBinding.imgSize.setImageResource(R.mipmap.ic_size_select)
                }

                SizeEnum.VENTI -> {
                    val sizeView = listViews.last()
                    sizeView.mBinding.cl.setBackgroundResource(R.drawable.bg_coffee_check)
                    sizeView.mBinding.imgSize.setImageResource(R.mipmap.ic_size_select)
                }

                else -> {
                    val sizeView = listViews.first()
                    sizeView.mBinding.cl.setBackgroundResource(R.drawable.bg_coffee_check)
                    sizeView.mBinding.imgSize.setImageResource(R.mipmap.ic_size_select)
                }
            }
            mBinding.tvPriceValue.text = it.getPrice()
            mBinding.coffeeName.text = it.name
            mBinding.tvDes.text = "A single espresso shot poured into hot foamy milk, with the surface topped with mildly sweetened cocoa powder and drizzled with scrumptious caramel syrup."
        }
    }

    override fun onEvent() {
        mBinding.title.imgBack.setOnClickListener {
            homeNavigator.popBack()
        }
        listViews.forEach { sizeView ->
            sizeView.mBinding.root.setOnClickListener {
                listViews.forEach {
                    it.mBinding.cl.setBackgroundResource(R.drawable.bg_coffee_un_check)
                    it.mBinding.imgSize.setImageResource(R.mipmap.ic_size_un_select)
                }
                sizeView.mBinding.cl.setBackgroundResource(R.drawable.bg_coffee_check)
                sizeView.mBinding.imgSize.setImageResource(R.mipmap.ic_size_select)
                when (sizeView.mBinding.tvSizeName.text) {
                    "Short" -> {
                        goodsBean?.sizeEnum = SizeEnum.SHORT
                    }

                    "Tall" -> {
                        goodsBean?.sizeEnum = SizeEnum.TALL
                    }

                    "Grande" -> {
                        goodsBean?.sizeEnum = SizeEnum.GRANDE
                    }

                    "Venti" -> {
                        goodsBean?.sizeEnum = SizeEnum.VENTI
                    }
                }

                goodsBean?.let {
                    mBinding.tvPriceValue.text = it.getPrice()
                }

            }
        }
        listExtra.forEach { extraView ->
            extraView.setOnClickListener {
                listExtra.forEach { it.setBackgroundResource(R.drawable.bg_coffee_un_check_12) }
                extraView.setBackgroundResource(R.drawable.bg_coffee_check_12)

                when (extraView.text) {
                    "SUGAR" -> {
                        goodsBean?.extraEnum = EXTRAEnum.SUGAR
                    }

                    "Milk" -> {
                        goodsBean?.extraEnum = EXTRAEnum.MILK
                    }
                }
                goodsBean?.let {
                    mBinding.tvPriceValue.text = it.getPrice()
                }
            }
        }
        listExtraType.forEach { extraExtraView ->
            extraExtraView.setOnClickListener {
                listExtraType.forEach { it.setBackgroundResource(R.drawable.bg_coffee_un_check_12) }
                extraExtraView.setBackgroundResource(R.drawable.bg_coffee_check_12)

                when (extraExtraView.text) {
                    "QAT MILK" -> {
                        goodsBean?.milktypeEnum = MILKTYPEEnum.OAT_MILK
                    }

                    "SOY MILK" -> {
                        goodsBean?.milktypeEnum = MILKTYPEEnum.SOY_MILK
                    }

                    "ALMOND MILK" -> {
                        goodsBean?.milktypeEnum = MILKTYPEEnum.ALMOND_MILK
                    }
                }
                goodsBean?.let {
                    mBinding.tvPriceValue.text = it.getPrice()
                }
            }
        }

        mBinding.placeOrder.setOnClickListener {
            setFragmentResult("CoffeeDetailFragment", bundleOf().apply { putParcelable("goods", goodsBean) })
            homeNavigator.popBack()
        }

    }
}